/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

/**
 *
 * @author Administrator
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import AnalysistImpl.ManagerAnalysist;
import AnalysistImpl.PressureAnalysist;
import AnalysistImpl.TemperatureAnalysist;
import AnalysistImpl.WindAnalysist;
import CommonValue.AlignPanelValue;
import CommonValue.Pressure;
import CommonValue.SystemSign;
import CommonValue.SystemTimer;
import CommonValue.Temperature;
import CommonValue.Wind;
import DTO.DrawDTO;
import DTO.PacketDTO;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class DrawCurrentWeatherGraphPanel1 extends JPanel implements Runnable {

    private Map<Date, Float> temperatureDataInHourMap;
    private Map<Date, Float> windDataInHourMap;
    private Map<Date, Float> pressureDataInHourMap;
    private PacketDTO hourlyReport = new PacketDTO(SystemSign.AdminCenter);
    private String locationWeather;
    private List<String> typeList;

    public DrawCurrentWeatherGraphPanel1(String locationWeather, List<String> typeList, PacketDTO hourlyReport) {
        this.locationWeather = locationWeather;
        this.typeList = typeList;
        this.hourlyReport = hourlyReport;
        //hourlyReport = new PacketDTO(SystemSign.AdminCenter);
        //temperatureDataInHourMap = new HashMap<Date, Float>();
        //windDataInHourMap = new HashMap<Date, Float>();
        //pressureDataInHourMap = new HashMap<Date, Float>();
    }

    public PacketDTO getHourlyReport() {
        return hourlyReport;
    }

    public void setHourlyReport(PacketDTO hourlyReport) {
        this.hourlyReport = hourlyReport;
    }

    public String getLocationWeather() {
        return locationWeather;
    }

    public void setLocationWeather(String locationWeather) {
        this.locationWeather = locationWeather;
    }

    public List<String> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<String> typeList) {
        this.typeList = typeList;
    }

    public Map<Date, Float> getTemperatureDataInHourMap(PacketDTO temp) {
        ManagerAnalysist analystManager = new ManagerAnalysist();
        analystManager.installNewAnalysist(new TemperatureAnalysist());
        DrawDTO temperatureData = analystManager.analysisReport(Temperature.SIGN, locationWeather, temp);
        temperatureDataInHourMap = new HashMap<Date, Float>();
        temperatureDataInHourMap = temperatureData.data.get(Temperature.TEMPERATURE);
        return temperatureDataInHourMap;
    }

    public Map<Date, Float> getPressureDataInHourMap(PacketDTO temp) {
        ManagerAnalysist analystManager = new ManagerAnalysist();
        analystManager.installNewAnalysist(new PressureAnalysist());
        DrawDTO pressureData = analystManager.analysisReport(Pressure.SIGN, locationWeather, temp);
        pressureDataInHourMap = new HashMap<Date, Float>();
        pressureDataInHourMap = pressureData.data.get(Pressure.PRESSURE);
        System.out.println("ppppppp " + pressureDataInHourMap.size());
        return pressureDataInHourMap;
    }

    public Map<Date, Float> getWindDataInHourMap(PacketDTO temp) {
        ManagerAnalysist analystManager = new ManagerAnalysist();
        analystManager.installNewAnalysist(new WindAnalysist());
        DrawDTO windData = analystManager.analysisReport(Wind.SIGN, locationWeather, temp);
        windDataInHourMap = new HashMap<Date, Float>();
        windDataInHourMap = windData.data.get(Wind.SPEED);
        return windDataInHourMap;
    }

    @Override
    public void paintComponent(Graphics g){
        paint(g);
    }
    public void paint(Graphics g) {
        super.paintComponent(g);

        /*if (temperatureDataInHourMap == null || temperatureDataInHourMap.size() == 0
                || windDataInHourMap == null || windDataInHourMap.size() == 0
                || pressureDataInHourMap == null || pressureDataInHourMap.size() == 0) {
            return;
        }*/
        int width = getWidth();
        int height = getHeight();
        //Ve truc ngang: thoi gian
        g.drawLine(AlignPanelValue.ALIGN_X_VALUE,
                height - AlignPanelValue.ALIGN_Y_VALUE,
                width,// - AlignPanelValue.ALIGN_X_VALUE,
                height - AlignPanelValue.ALIGN_Y_VALUE);

        //Ve truc doc: gia tri
        g.drawLine(AlignPanelValue.ALIGN_X_VALUE,
                height - AlignPanelValue.ALIGN_Y_VALUE,
                AlignPanelValue.ALIGN_X_VALUE,
                AlignPanelValue.ALIGN_Y_VALUE);

        //chia truc ngang thanh stepX phan bang nhau
        int stepX = (width - 2 * AlignPanelValue.ALIGN_X_VALUE) / 60;

        //Chia truc doc thanh stepY phan bang nhau
        int stepY = (height - 2 * AlignPanelValue.ALIGN_Y_VALUE) / 6;

        //khoang cach tu mep duoi panel toi truc ngang
        int heightDiff = height - AlignPanelValue.ALIGN_Y_VALUE;

        //Ve cac gia tri tren truc doc
        drawYValues(g, heightDiff, stepY);

        //Ve cac gia tri tren truc ngang
        drawXValues(g, AlignPanelValue.ALIGN_X_VALUE, heightDiff, stepX);

        PacketDTO temp = new PacketDTO("Drawing");
        System.out.println("hourlyReport size " + hourlyReport.getBuffer().size());
        synchronized (hourlyReport) {
            temp.clearBuffer();
            temp.addAllToBuffer(hourlyReport.getBuffer());
            System.out.println("size " + temp.getBuffer().size());
        }

        for (int i = 0; i < typeList.size(); i++) {
            String typeWeather = typeList.get(i);
            System.out.println("typeWeather " + typeWeather);
            if (typeWeather.equals(Temperature.SIGN)) //Ve duong nhiet do
            {
                getTemperatureDataInHourMap(temp);
                drawTemperatureLine(g, stepX, heightDiff, stepY);
            } else if (typeWeather.equals(Wind.SIGN)) //Ve duong toc do gio
            {
                getWindDataInHourMap(temp);
                drawWindLine(g, stepX, heightDiff, stepY);
            } else if (typeWeather.equals(Pressure.SIGN)) //Ve duong ap suat
            {
                System.out.println("ve duong ap suat");
                getPressureDataInHourMap(temp);
                drawPressureLine(g, stepX, heightDiff, stepY);
            }
            //Ve ten duong
            //drawLineName(typeWeather, g, width, height);
        }
    }

    private void drawYValues(Graphics g, int heightDiff, int stepY) {
        g.setColor(Color.black);
        for (int i = 0; i <= 6; i++) {
            g.drawString(Integer.toString(i * 20),
                    AlignPanelValue.ALIGN_X_VALUE - 20,
                    heightDiff - i * stepY);
        }
    }

    private void drawXValues(Graphics g, int widthDiff, int heightDiff, int stepX) {
        g.setColor(Color.black);
        for (int i = 0; i <= 12; i++) {
            g.drawString(Integer.toString(i * 5),
                    widthDiff + i * stepX * 5,
                    heightDiff + 20);
        }
    }

    private void drawLineName(String typeWeather, Graphics g, int width, int height) {
        if (typeWeather.equals(Temperature.SIGN)) {
            //Nhiet do
            g.setColor(Color.red);
            g.drawString("Temperature", width - 100, AlignPanelValue.DRAW_LINE_NAME_Y);
            g.drawLine(width - 150, AlignPanelValue.DRAW_LINE_NAME_Y, width - 120,
                    AlignPanelValue.DRAW_LINE_NAME_Y);

        } else if (typeWeather.equals(Wind.SIGN)) {
            //Toc do gio
            g.setColor(Color.black);
            g.drawString("Wind speed", width - 100, AlignPanelValue.DRAW_LINE_NAME_Y
                    + AlignPanelValue.LINE_NAME_DISTANCE);
            g.drawLine(width - 150, 80, width - 120, 80);
        } else if (typeWeather.equals(Pressure.SIGN)) {
            //Ap suat
            g.setColor(Color.green);
            g.drawString("Pressure", width - 100, AlignPanelValue.DRAW_LINE_NAME_Y
                    + 2 * AlignPanelValue.LINE_NAME_DISTANCE);
            g.drawLine(width - 150, 110, width - 120, 110);
        }
    }

    private void drawTemperatureLine(Graphics g, int stepX, int heightDiff, int stepY) {
        List<Date> dateList = new ArrayList<Date>(temperatureDataInHourMap.keySet());
        for (int i = 1; i < temperatureDataInHourMap.size(); i++) {
            Date i1Date = dateList.get(i - 1);
            Date i2Date = dateList.get(i);
            float temper1 = Math.abs(temperatureDataInHourMap.get(i1Date));
            float temper2 = Math.abs(temperatureDataInHourMap.get(i2Date));

            System.out.println("temper1 " + temper1);
            System.out.println("temper2 " + temper2);

            //Toa do diem dau
            int x1 = AlignPanelValue.ALIGN_X_VALUE + (i - 1) * stepX;
            int y1 = Math.abs(heightDiff - ((int) temper1) * stepY / 20);

            System.out.println("x1 " + x1);
            System.out.println("y1 " + y1);

            //Toa do diem cuoi
            int x2 = AlignPanelValue.ALIGN_X_VALUE + i * stepX;
            int y2 = Math.abs(heightDiff - ((int) temper2) * stepY / 20);

            System.out.println("x2 " + x2);
            System.out.println("y2 " + y2);

            g.setColor(Color.red);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private void drawWindLine(Graphics g, int stepX, int heightDiff, int stepY) {
        List<Date> dateList = new ArrayList<Date>(windDataInHourMap.keySet());
        for (int i = 1; i < windDataInHourMap.size(); i++) {
            Date i1Date = dateList.get(i - 1);
            Date i2Date = dateList.get(i);
            float speed1 = windDataInHourMap.get(i1Date);
            float speed2 = windDataInHourMap.get(i2Date);

            System.out.println("speed1 " + speed1);
            System.out.println("speed2 " + speed2);

            //Toa do diem dau
            int x1 = AlignPanelValue.ALIGN_X_VALUE + (i - 1) * stepX;
            int y1 = Math.abs(heightDiff - ((int) speed1) * stepY / 20);

            System.out.println("x1 " + x1);
            System.out.println("y1 " + y1);

            //Toa do diem cuoi
            int x2 = AlignPanelValue.ALIGN_X_VALUE + i * stepX;
            int y2 = Math.abs(heightDiff - ((int) speed2) * stepY / 20);

            System.out.println("x2 " + x2);
            System.out.println("y2 " + y2);

            g.setColor(Color.black);
            g.drawLine(x1, y1, x2, y2);
        }
    }

    private void drawPressureLine(Graphics g, int stepX, int heightDiff, int stepY) {
        List<Date> dateList = new ArrayList<Date>(pressureDataInHourMap.keySet());
        for (int i = 1; i < pressureDataInHourMap.size(); i++) {
            Date i1Date = dateList.get(i - 1);
            Date i2Date = dateList.get(i);
            float pressure1 = pressureDataInHourMap.get(i1Date);
            float pressure2 = pressureDataInHourMap.get(i2Date);

            System.out.println("pressure1 " + pressure1);
            System.out.println("pressure2 " + pressure2);

            //Toa do diem dau
            int x1 = AlignPanelValue.ALIGN_X_VALUE + (i - 1) * stepX;
            int y1 = Math.abs(heightDiff - ((int) pressure1) * stepY / 20);

            System.out.println("x1 " + x1);
            System.out.println("y1 " + y1);

            //Toa do diem cuoi
            int x2 = AlignPanelValue.ALIGN_X_VALUE + i * stepX;
            int y2 = Math.abs(heightDiff - ((int) pressure2) * stepY / 20);

            System.out.println("x2 " + x2);
            System.out.println("y2 " + y2);

            g.setColor(Color.GREEN);
            g.drawLine(x1, y1, x2, y2);
        }
    }
    
    @Override
    public void run() {
        while(true){
            System.out.println("drawnnnnnnnnn");

            paint(this.getGraphics());
            try {
                
                Thread.sleep(SystemTimer.CheckReportSleep);
            } catch (InterruptedException ex) {
                Logger.getLogger(DrawCurrentWeatherGraphPanel1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

